home *** CD-ROM | disk | FTP | other *** search
/ CD Actual 90 / CD Actual 90.iso / Software3D / K-3D / k3d-0.4.2.1 / shaders / k3d_water.sl < prev    next >
Encoding:
Text File  |  2004-07-23  |  3.5 KB  |  111 lines

  1. /*
  2.  * water.sl -- water surface, using ray tracing.
  3.  * This was originally shiny.sl, but I change it to make it transparant.  The interesting part colors that
  4.  * are not blue will pass thru the water.
  5.  *
  6.  * note : This is very similar to shiny.sl with a slight modification to give it a
  7.  *        water like look.
  8.  *
  9.  * modified by Lawrence D. Chin, cs184-bo
  10.  */
  11.  
  12. /* DESCRIPTION:
  13.  *   Makes a smoothly polished metal, using ray tracing to calculate
  14.  *   reflections of the environment.
  15.  * 
  16.  * PARAMETERS:
  17.  *    Ka, Kd, Ks, roughness, specularcolor - The usual meaning
  18.  *    Kr - coefficient for mirror-like reflections of environment
  19.  *    blur - how blurry are the reflections? (0 = perfectly sharp)
  20.  *    samples - set to higher than 1 for oversampling of blur
  21.  *
  22.  *
  23.  * HISTORY:
  24.  *      Aug 1991 -- written by lg in C
  25.  *      25 Jan 1994 -- recoded by lg in correct shading language.
  26.  *
  27.  * last modified 25 Jan 1994 by Larry Gritz
  28.  */
  29.  
  30. #define pulse(a,b,fuzz,x) (smoothstep((a)-(fuzz),(a),(x)) - \
  31.                            smoothstep((b)-(fuzz),(b),(x)))
  32.  
  33. #define blend(a,b,x) ((a) * (1 - (x)) + (b) * (x))
  34.  
  35. surface
  36. k3d_water ( float Ka = 0, Kd = 0, Ks = 1;
  37.     float Kr = 1, roughness = 0, blur = 0;
  38.     color specularcolor = 1;
  39.     float samples = 0,
  40.         radius = 8,           /* radius of ring */
  41.         half_width = 0.1;      /* half width of ring */
  42.       )
  43. {
  44.     /* FIRST LAYER */
  45.     
  46.     normal Nf;               /* Forward facing normal vector */
  47.     vector IN;               /* normalized incident vector */
  48.     vector uoffset, voffset; /* Offsets for blur */
  49.     color surface_color,     /* Resulting color */
  50.           ev;                /* Color of the reflections */
  51.     vector R, Rdir;          /* Direction to cast the ray */
  52.     uniform float i, j;
  53.     
  54.     /* Construct a forward facing surface normal */
  55.     Nf = faceforward (normalize(N), I);
  56.     IN = normalize (I);
  57.     ev = 0;
  58.  
  59.     /* Calculate the reflection color */
  60.     if (Kr > 0.001) {
  61.     /* Rdir gets the perfect reflection direction */
  62.     Rdir = normalize (reflect (IN, Nf));
  63.     if (blur > 0) {
  64.         /* Construct orthogonal components to Rdir */
  65.         uoffset = blur * normalize (vector (zcomp(Rdir) - ycomp(Rdir),
  66.                             xcomp(Rdir) - zcomp(Rdir),
  67.                             ycomp(Rdir) - xcomp(Rdir)));
  68.         voffset = Rdir ^ uoffset;
  69.         for (i = 0;  i < samples;  i += 1) {
  70.         for (j = 0;  j < samples;  j += 1) {
  71.             /* Add a random offset to the smooth reflection vector */
  72.             R = Rdir +
  73.             ((i + float random())/samples - 0.5) * uoffset +
  74.             ((j + float random())/samples - 0.5) * voffset;
  75.             ev += trace (P, normalize(R));
  76.         }
  77.         }
  78.         ev *= Kr / (samples*samples);
  79.     } else {
  80.         /* No blur, just do a simple trace */
  81.         ev = Kr * trace (P, Rdir);
  82.     }
  83.     }
  84.  
  85.     surface_color = Os * ( Cs * (Ka*ambient() + Kd*diffuse(Nf)) +
  86.         specularcolor * (ev + Ks*specular(Nf,-IN,roughness)));
  87.  
  88.     /* I added some current.  Note, it doesn't apppear horizontally. */
  89.     color current = (1,1,1);  
  90.    
  91.     color layer_color = (1,1,1);
  92.     color layer_opac = 1;
  93.     float fuzz = 0.025;
  94.     point center;
  95.     float d;
  96.  
  97.     center = (0.5, 0.5, 0);  /* position of ring */
  98.     d = distance(center, (s, t, 0));
  99.     layer_opac = pulse(radius - half_width, radius + half_width, fuzz, d);
  100.     surface_color = blend(surface_color, layer_color, layer_opac);
  101.  
  102.     Ci = surface_color;
  103.      
  104.     /* This gives the water a somewhat transparant look. 
  105.      *   Note: This does works with radiosity.
  106.      */
  107.  
  108.     Oi = (0, 0, 0.0000001);
  109.     
  110. }
  111.